package nayasis.common.db.manager.sql;

import java.sql.Types;
import java.util.List;

import nayasis.common.util.StringUtil;

/**
 * Parameter Value to bind in SQL
 *
 * @author nayasis
 */
public class ParamValue {

    /**
     * Parameter Type
     * @see java.sql.Types
     */
    private int type;

    /**
     * Parameter Value
     */
    private Object value;

    /**
     * Default Constructor
     *
     * @param value value to set
     */
    public ParamValue( Object value ) {
        setValue( value );
    }

    public void setValue( Object value ) {

        this.type  = getType( value );
        this.value = value;

    }

    public Object getValue() {
        return this.value;
    }

    public int getType() {
        return this.type;
    }

    public boolean isArray() {
        return type == Types.ARRAY;
    }

    public boolean isInteger() {
        return type == Types.INTEGER;
    }

    public boolean isFloat() {
        return type == Types.FLOAT;
    }

    public boolean isDouble() {
        return type == Types.DOUBLE;
    }

    public boolean isVarchar() {
        return type == Types.VARCHAR;
    }

    public boolean isNull() {
        return type == Types.NULL;
    }

    public boolean isNumeric() {
        return type == Types.INTEGER || type == Types.FLOAT || type == Types.DOUBLE;
    }
    
    public String toString() {
        return StringUtil.format( "[type : @, value : @\n]", type, value );
    }

    /**
     * get type infomation from object
     *
     * @param object
     * @return java.sql.Types
     */
    private int getType( Object object ) {

        int type = Types.NULL;

        if( object == null ) {
            type = Types.NULL;
        } else if( object instanceof Short ) {
            type = Types.INTEGER;
        } else if( object instanceof Integer ) {
            type = Types.INTEGER;
        } else if( object instanceof Long ) {
            type = Types.INTEGER;
        } else if( object instanceof Float ) {
            type = Types.FLOAT;
        } else if( object instanceof Double ) {
            type = Types.DOUBLE;
        } else if( object instanceof String ) {
            type = Types.VARCHAR;
        } else if( object instanceof List ) {
            type = Types.ARRAY;
        }

        return type;

    }

}